/*
 * Copyright @ 2024 weili23
 * com.example.mythread.a04threadcase 9:23
 * All right reserved.
 */

package com.example.mythread.a05threadmethod;

import com.example.mythread.a04threadmethod.MyThread;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2024/10/17 9:23
 * @version: v1.0
 */
public class MyRun implements Runnable{
    /*
    * 100张票 抢完为止  同步方法实现
     */
    int tickets = 0;
    @Override
    public void run() {
        while (true){
            if(getTicket()){
                break;
            }

        }
    }


    private synchronized boolean getTicket(){
        if (tickets < 100){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tickets++;
            System.out.println(Thread.currentThread().getName() + "正在卖第" + tickets + "张票");
            return false;
        }else{
            return true;
        }
    }
}
